//
//  PaiHangViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/6.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "PaiHangViewController.h"
#import "PaiHangCell.h"
#import "PaiHangModel.h"

@interface PaiHangViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列
@property (nonatomic,retain)UITableView *mainTableView; // 主tableView

// 添加pageIndex
@property (nonatomic,retain) NSNumber *pageIndex;


@end

@implementation PaiHangViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageIndex = @1;
    self.view.backgroundColor = [UIColor whiteColor];

    self.modelArr = [NSMutableArray array];
    [self createTableView];
    [self addRefresh];
    [self requestData];
    // Do any additional setup after loading the view.
}

// 添加刷新

- (void)addRefresh{
    //下拉刷新
    [self.mainTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        
        [self.modelArr removeAllObjects];
        self.pageIndex = @1;
        [self requestNewData];
    }];
    
    
    // 上拉加载
    [self.mainTableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
      
        NSInteger number = [self.pageIndex integerValue];
        number++;
        self.pageIndex = [NSNumber numberWithInteger:number];
        [self requestNewData];
    }];
    
    
}




- (void)createTableView
{
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height) style:UITableViewStylePlain];
    [_mainTableView registerClass:[PaiHangCell class] forCellReuseIdentifier:@"paihangcell"];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    [_mainTableView release];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PaiHangModel *model;
    PaiHangCell *cell = [tableView dequeueReusableCellWithIdentifier:@"paihangcell" forIndexPath:indexPath];
    if (self.modelArr.count>0) {
        model = [self.modelArr objectAtIndex:indexPath.row];
        cell.model = model;
    }
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    PaiHangModel *model;
    if (self.modelArr.count > 0) {
    model = self.modelArr[indexPath.row];
        return [PaiHangCell heightForCell:model];

    }else
        return 0;
    
    
}


// AFN 数据请求
- (void)requestData
{
    NSString *str = [NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=1&topListId=%@",self.list];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"movies"];
        for (NSDictionary *dic in arr) {
            PaiHangModel *model = [[PaiHangModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
        }
        [self.mainTableView reloadData];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}

// AFN 数据请求
- (void)requestNewData
{
    NSString *str = [NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=%@&topListId=%@",self.pageIndex, self.list];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"movies"];
        for (NSDictionary *dic in arr) {
            PaiHangModel *model = [[PaiHangModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
        }
        [self.mainTableView.defaultFooter endRefreshing];
        [self.mainTableView.defaultHeader endRefreshing];
        [self.mainTableView reloadData];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}








-(void)dealloc
{
    [_pageIndex release];
    [_list release];
    [_mainTableView release];
    [_operation release];
    [_queue release];
    [_modelArr release];
    [super dealloc];
}


@end
